import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import mockjs from 'mockjs'
import type {Plugin} from "vite";
import url from 'node:url'
// vite插件  模拟一个fetch请求mock数据
const viteMockServer = ():Plugin => {
    return  {
      name:'vite-mock-server',
      configureServer(server){
        server.middlewares.use('/api/list',(req,res) => {
          const parseUrl = url.parse(req.originalUrl,true).query
          res.setHeader('Content-Type', 'application/json')
          // xxxxx?page=xxx&no=xxx    加了true就会变成对象形式{}
          const data = mockjs.mock({
            'list|1000':[
              {
                "id|+1": 1, //id自增
                "name": parseUrl.keyWord, //name为url参数中的keyWord
                'address': '@county(true)', //address为随机地址
              }
            ]
          })
          res.end(JSON.stringify(data))
        })
      }
    }
}
// https://vite.dev/config/
export default defineConfig({
  plugins: [react(),viteMockServer()],
})
